/*****************************************************************************************
 **  Author:COCO 2022
 *****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码

let extension = {
  components: {
    //查询界面扩展组件
    gridHeader: "",
    gridBody: "",
    gridFooter: "",
    //新建、编辑弹出框扩展组件
    modelHeader: "",
    modelBody: "",
    modelFooter: "",
  },
  tableAction: "", //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    onInit() {
      //框架初始化配置前
      this.columnIndex = true;
      this.boxOptions.height = 450;

      // 添加调试信息
      console.log('onInit - editFormFields:', this.editFormFields);
      console.log('onInit - editFormOptions:', this.editFormOptions);
    },
    onInited() {
      //框架初始化配置后
      console.log('onInited - editFormFields:', this.editFormFields);
      console.log('onInited - editFormOptions:', this.editFormOptions);
    },
    searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      // 新建保存前的数据处理
      console.log('addBefore formData:', formData);
      console.log('addBefore editFormFields:', this.editFormFields);

      // 检查表单数据是否正确映射
      if (formData && typeof formData === 'object') {
        // 尝试从editFormFields获取数据
        const formValues = {
          Mass_Defects_Name: this.editFormFields.Mass_Defects_Name || formData.Mass_Defects_Name,
          Mass_Defects_Type: this.editFormFields.Mass_Defects_Type || formData.Mass_Defects_Type,
          Mass_Defects_Level: this.editFormFields.Mass_Defects_Level || formData.Mass_Defects_Level,
          Mass_Defects_Remark: this.editFormFields.Mass_Defects_Remark || formData.Mass_Defects_Remark
        };

        console.log('Processed form values:', formValues);

        // 确保数据存在且不为空
        if (!formValues.Mass_Defects_Name || formValues.Mass_Defects_Name.trim() === '') {
          this.$message.error('质量缺陷描述不能为空');
          return false;
        }
        if (!formValues.Mass_Defects_Type || formValues.Mass_Defects_Type.trim() === '') {
          this.$message.error('质量缺陷类型不能为空');
          return false;
        }
        if (!formValues.Mass_Defects_Level || formValues.Mass_Defects_Level.trim() === '') {
          this.$message.error('质量缺陷等级不能为空');
          return false;
        }

        // 更新formData
        Object.assign(formData, formValues);
      } else {
        console.error('formData is invalid:', formData);
        this.$message.error('表单数据无效');
        return false;
      }
      return true;
    },
    updateBefore(formData) {
      // 编辑保存前的数据处理
      console.log('updateBefore formData:', formData);
      console.log('updateBefore editFormFields:', this.editFormFields);

      // 检查表单数据是否正确映射
      if (formData && typeof formData === 'object') {
        // 尝试从editFormFields获取数据
        const formValues = {
          Mass_Defects_Name: this.editFormFields.Mass_Defects_Name || formData.Mass_Defects_Name,
          Mass_Defects_Type: this.editFormFields.Mass_Defects_Type || formData.Mass_Defects_Type,
          Mass_Defects_Level: this.editFormFields.Mass_Defects_Level || formData.Mass_Defects_Level,
          Mass_Defects_Remark: this.editFormFields.Mass_Defects_Remark || formData.Mass_Defects_Remark
        };

        console.log('Processed form values:', formValues);

        // 确保数据存在且不为空
        if (!formValues.Mass_Defects_Name || formValues.Mass_Defects_Name.trim() === '') {
          this.$message.error('质量缺陷描述不能为空');
          return false;
        }
        if (!formValues.Mass_Defects_Type || formValues.Mass_Defects_Type.trim() === '') {
          this.$message.error('质量缺陷类型不能为空');
          return false;
        }
        if (!formValues.Mass_Defects_Level || formValues.Mass_Defects_Level.trim() === '') {
          this.$message.error('质量缺陷等级不能为空');
          return false;
        }

        // 更新formData
        Object.assign(formData, formValues);
      } else {
        console.error('formData is invalid:', formData);
        this.$message.error('表单数据无效');
        return false;
      }
      return true;
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      // this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    modelOpenAfter(row) {
      console.log('modelOpenAfter row:', row);
      console.log('modelOpenAfter editFormFields:', this.editFormFields);

      // 编辑时数据反填
      if (this.currentAction !== 'Add') {
        // 编辑模式，反填数据
        if (row) {
          this.editFormFields.Mass_Defects_Name = row.Mass_Defects_Name || '';
          this.editFormFields.Mass_Defects_Type = row.Mass_Defects_Type || '';
          this.editFormFields.Mass_Defects_Level = row.Mass_Defects_Level || '';
          this.editFormFields.Mass_Defects_Remark = row.Mass_Defects_Remark || '';

          console.log('After data binding - editFormFields:', this.editFormFields);
        }
      } else {
        // 新建模式，清空表单
        this.editFormFields.Mass_Defects_Name = '';
        this.editFormFields.Mass_Defects_Type = '';
        this.editFormFields.Mass_Defects_Level = '';
        this.editFormFields.Mass_Defects_Remark = '';

        console.log('After clearing form - editFormFields:', this.editFormFields);
      }
    },
  },
};
export default extension;
